/***************************************************************************
 *   Copyright (C) 2009 by Zheng Yonglei
 *   zhengyonglei@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "ReciteGREWords.h"

ReciteGREWords::ReciteGREWords(QWidget *parent)
    : QMainWindow(parent),
    mTimer(NULL),
    mTime(QTime(0,0,0)),
    mStartTimer(false)
{
	mRefreshTimer = new QTimer(this);
	OPT.refreshTimer = mRefreshTimer;
	QObject::connect(mRefreshTimer, SIGNAL(timeout()), this, SLOT(update()));
	if (OPT.refreshInterval > 0)
		mRefreshTimer->start(OPT.refreshInterval);

	ui.setupUi(this);

	QObject::connect(ui.actionSettings, SIGNAL(triggered()), this, SLOT(openSettingDialog()));
	QObject::connect(ui.actionAbout, SIGNAL(triggered()), this, SLOT(openAboutThisProgram()));

	mLoginDialog = new LoginDialog();
	//connect(mLoginDialog, SIGNAL(loginSig()), this, SLOT(login()));

	QShortcut* spaceBarShortcut = new QShortcut(QKeySequence(Qt::Key_Space),  this);
	QObject::connect(spaceBarShortcut,SIGNAL(activated()),this,SLOT(spaceBarPressed()));

	//set manu bar
	mImportData = new QAction(QIcon("./pixmaps/db_comit-256.png"), tr("Import data"), this);
	mImportData->setStatusTip(tr("Import data"));
	ui.menuTools->addAction(mImportData);
	connect(mImportData, SIGNAL(triggered()), this, SLOT(openImportDataDialog()));

	mExportData = new QAction(QIcon("./pixmaps/db_update-256.png"), tr("Export data"), this);
	mExportData->setStatusTip(tr("Export data"));
	ui.menuTools->addAction(mExportData);
	connect(mExportData, SIGNAL(triggered()), this, SLOT(openExportDataDialog()));

	//set tool bar
	mOpenListAct = new QAction(QIcon("./pixmaps/fileopen-256.png"), tr("Open List"), this);
	mOpenListAct->setStatusTip(tr("Open a list."));
	connect(mOpenListAct, SIGNAL(triggered()), this, SLOT(openSelectListDialog()));

	mOpenAnalysisDialogAct = new QAction(QIcon("./pixmaps/kchart.png"), tr("Open Reciting Analysis Dialog"), this);
	mOpenAnalysisDialogAct->setStatusTip(tr("Open Reciting Analysis Dialog"));
	connect(mOpenAnalysisDialogAct, SIGNAL(triggered()), this, SLOT(openAnalysisDialog()));

	mResumeAct = new QAction(QIcon("./pixmaps/forward-256.png"), tr("Resume Last Task"), this);
	mResumeAct->setStatusTip(tr("Resume last task."));
	connect(mResumeAct, SIGNAL(triggered()), this, SLOT(resume()));

	mStartAct = new QAction(QIcon("./pixmaps/player_play-256.png"), tr("Start"), this);
	mStartAct->setStatusTip(tr("Start current task."));
	connect(mStartAct, SIGNAL(triggered()), this, SLOT(startButtonPushed()));

	mPauseAct = new QAction(QIcon("./pixmaps/player_pause-256.png"), tr("Pause"), this);
	mPauseAct->setStatusTip(tr("Pause current task."));
	connect(mPauseAct, SIGNAL(triggered()), this, SLOT(pauseButtonPushed()));

	mFinishedCurTaskAct = new QAction(QIcon("./pixmaps/player_stop-256.png"), tr("Finish"), this);
	mFinishedCurTaskAct->setStatusTip(tr("Finish Current Task."));
	connect(mFinishedCurTaskAct, SIGNAL(triggered()), this, SLOT(finishCurTask()));

	mSwitchTaskAct = new QAction(QIcon("./pixmaps/agt_reload-256.png"), tr("Switch Task"), this);
	mSwitchTaskAct->setStatusTip(tr("Switch between recite task and browse task"));
	connect(mSwitchTaskAct, SIGNAL(triggered()), this, SLOT(switchTask()));

	mShowLogApp = new QAction(QIcon("./pixmaps/mail_find-256.png"), tr("Show Logger"), this);
	mShowLogApp->setStatusTip(tr("Open Logger Window."));
	connect(mShowLogApp, SIGNAL(triggered()), this, SLOT(openWinLogWidget()));

	mZoomIn = new QAction(QIcon("./pixmaps/viewmag+-256.png"), tr("Zoom In"), this);
	mZoomIn->setStatusTip(tr("Enlarge Font"));
	//mZoomIn->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Plus));
	connect(mZoomIn, SIGNAL(triggered()), this, SLOT(zoomIn()));

	mZoomOut = new QAction(QIcon("./pixmaps/viewmag--256.png"), tr("Zoom Out"), this);
	mZoomOut->setStatusTip(tr("Reduce Font"));
	//mZoomOut->setShortcut(QKeySequence::ZoomOut);
	connect(mZoomOut, SIGNAL(triggered()), this, SLOT(zoomOut()));

	mShowLoginDialog = new QAction(QIcon("./pixmaps/identity-256.png"), tr("Open Login Dialog"), this);
	mShowLoginDialog->setStatusTip(tr("Open login dialog"));
	connect(mShowLoginDialog, SIGNAL(triggered()), this, SLOT(openLoginDialog()));

	/*mFindWordInDB = new QAction(QIcon("./pixmaps/find-256.png"), tr("Find Word"), this);
	mFindWordInDB->setStatusTip(tr("Find word in database."));
	connect(mFindWordInDB, SIGNAL(triggered()), this, SLOT(findWordInDB()));
	mFindWordInDB->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_F));*/

	mExitApp = new QAction(QIcon("./pixmaps/application-exit.png"), tr("Exit"), this);
	mExitApp->setStatusTip(tr("Exit ReciteGREWords"));
	connect(mExitApp, SIGNAL(triggered()), this, SLOT(exitApp()));

	ui.toolBar->addAction(mResumeAct);
	ui.toolBar->addSeparator();
	//ui.toolBar->addAction(mShowLogApp);
	ui.toolBar->addAction(mStartAct);
	ui.toolBar->addAction(mPauseAct);
	ui.toolBar->addAction(mFinishedCurTaskAct);
	//ui.toolBar->addAction(mSwitchTaskAct); !!! unstable function !!!
	ui.toolBar->addSeparator();

	ui.toolBar->addAction(mZoomIn);
	ui.toolBar->addAction(mZoomOut);

	ui.toolBar->addSeparator();
	ui.toolBar->addAction(mShowLoginDialog);
	ui.toolBar->addAction(mOpenAnalysisDialogAct);
	ui.toolBar->addAction(mOpenListAct);

	//ui.toolBar->addSeparator();
	//ui.toolBar->addAction(mFindWordInDB);

	mTimeElapsedEditor = new QTimeEdit(this);
	mTimeElapsedEditor->setDisplayFormat(QString("h:mm:ss"));
	mTimeElapsedEditor->setTime(QTime(0,0,0));
	mTimeElapsedEditor->setReadOnly(true);
	ui.toolBar->addSeparator();
	ui.toolBar->addWidget(mTimeElapsedEditor);

	mMainBulletin = new QTextEdit(this);
	mMainBulletin->setReadOnly(true);
	mMainBulletin->setMaximumHeight(35);
	ui.toolBar->addWidget(mMainBulletin);

	ui.toolBar->addSeparator();
	ui.toolBar->addAction(mExitApp);

	mSelectListDialog = new SelectListDialog();
	mSettingsDialog = new SettingsDialog();
	mReciteAnalysisDialog = new ReciteAnalysisDialog();
	mSearchDBDialog = new SearchDB();
	mExportDataDialog = new ExportDataDialog();
	mImportDataDialog = new ImportDataDialog();

	QObject::connect(mReciteAnalysisDialog, SIGNAL(reciteSelectedWords()), this, SLOT(reciteTaskSelected()));
	QObject::connect(mReciteAnalysisDialog, SIGNAL(browseSelectedWords()), this, SLOT(browseTaskSelected()));
	QObject::connect(mSelectListDialog, SIGNAL(reciteTask()), this, SLOT(reciteTaskSelected()));
	QObject::connect(mSelectListDialog, SIGNAL(browseTask()), this, SLOT(browseTaskSelected()));


	mBrowseWordsWidget = new BrowseWordsWidget(this);
	mBrowseWordsWidget->hide();

	mReciteWordsWidget = new ReciteWordsWidget();
	mReciteWordsWidget->hide();

	mCentralGrid = new QGridLayout(ui.centralwidget);
	mCentralGrid->addWidget(mBrowseWordsWidget, 0, 0);
	mCentralGrid->addWidget(mReciteWordsWidget, 0, 0);

	connect(	mReciteWordsWidget, SIGNAL(updateMainBulletin()),
					this, SLOT(updateMainBulletin()));
	connect(	mReciteWordsWidget->getWordBrowser(), SIGNAL(searchWord(QString)),
					this, SLOT(findWordInDB(QString)));

	QObject::connect(mReciteWordsWidget, SIGNAL(finishCurTask()), this, SLOT(finishCurTask()));
	QObject::connect(mReciteWordsWidget, SIGNAL(openSelectListDialog()), this, SLOT(openSelectListDialog()));
	QObject::connect(mReciteWordsWidget, SIGNAL(openAnalysisDialog()), this, SLOT(openAnalysisDialog()));

	//initConnections();
	//actions

	//connect(ui.actionAbout, SIGNAL(triggered()), this, SLOT(openAboutDialog()));

	//buttons in task select dialog

	//QFont browserFont = mBrowseWordsWidget->browserDiaplay()->currentFont();
	//browserFont.setPointSize(OPT.browseDisplayFontSize);
	mBrowseWordsWidget->browserDiaplay()->setFont(OPT.wordBrowserFont);

	//QFont reciteFont = mReciteWordsWidget->reciteDiaplay()->currentFont();
	//reciteFont.setPointSize(OPT.reciteDisplayFontSize);
	mReciteWordsWidget->reciteDiaplay()->setFont(OPT.wordBrowserFont);

	mOpenListAct->setEnabled(false);
	mOpenAnalysisDialogAct->setEnabled(false);
	mResumeAct->setEnabled(false);
	mStartAct->setEnabled(false);
	mPauseAct->setEnabled(false);
	mFinishedCurTaskAct->setEnabled(false);
	mShowLogApp->setEnabled(false);
	mZoomIn->setEnabled(false);
	mZoomOut->setEnabled(false);

	//mLanguageActionGroup = new QActionGroup(this);
	//QObject::connect(mLanguageActionGroup, SIGNAL(triggered(QAction*)), this, SLOT(languageSelected(QAction*)));

	readSettings();
	//loadLanguages();
	//mFindWordInDB->setEnabled(false);
}

ReciteGREWords::~ReciteGREWords()
{
	//cout<<"ReciteGREWords::~ReciteGREWords"<<endl;
	writeSettings();
}

void ReciteGREWords::readSettings()
{
	QSettings settings("./configure/ReciteGREWords.conf" , QSettings::IniFormat );
	QVariant val;
	settings.beginGroup( "MainWindow" );

	val = settings.value("MainWindowStatus");
	if(val.isValid())
	{
		if (val.toString() == "Qt::WindowMaximized")
			OPT.mainWindowStatus = Qt::WindowMaximized;
		else
			OPT.mainWindowStatus = Qt::WindowNoState;
	}

	val = settings.value("MainWindowHeight");
	if(val.isValid()) OPT.mainWindowHeight = val.toInt();

	val = settings.value("MainWindowWidth");
	if(val.isValid()) OPT.mainWindowWidth = val.toInt();

	val = settings.value("MainWindowX");
	if(val.isValid()) OPT.mainWindowX = val.toInt();

	val = settings.value("MainWindowY");
	if(val.isValid()) OPT.mainWindowY = val.toInt();

	settings.endGroup();
}

void ReciteGREWords::writeSettings()
{
	//cout<<"ReciteGREWords::writeSettings"<<endl;
	QSettings settings("./configure/ReciteGREWords.conf" , QSettings::IniFormat );
	QVariant val;
	settings.beginGroup( "MainWindow" );

	switch(OPT.mainWindowStatus)
	{
	case Qt::WindowMaximized:
		settings.setValue("MainWindowStatus", "Qt::WindowMaximized"); break;
	default:
		settings.setValue("MainWindowStatus", "Qt::WindowNoState"); break;
	}

	settings.setValue("MainWindowHeight", QString("%1").arg(OPT.mainWindowHeight));
	settings.setValue("MainWindowWidth", QString("%1").arg(OPT.mainWindowWidth));
	settings.setValue("MainWindowX", QString("%1").arg(OPT.mainWindowX));
	settings.setValue("MainWindowY", QString("%1").arg(OPT.mainWindowY));

	settings.endGroup();
}

/*void ReciteGREWords::loadLanguages()
{
	QAction* EnglishAction = new QAction("English", this);
	EnglishAction->setCheckable(true);
	mLanguageMap[EnglishAction].language = QString("English");
	mLanguageMap[EnglishAction].action = EnglishAction;
	//mLanguageMap["English"].translator = NULL;
	mLanguageMap[EnglishAction].isValid = true;
	mLanguageMap[EnglishAction].isDefault = true;

	QAction* ChineseAction = new QAction("Chinese", this);
	ChineseAction->setCheckable(true);
	mLanguageMap[ChineseAction].language = QString("Chinese");
	mLanguageMap[ChineseAction].action = ChineseAction;
	mLanguageMap[ChineseAction].translator = new QTranslator();
	if (mLanguageMap[ChineseAction].translator->load("ReciteGREWords_zh_CN.qm"))
		mLanguageMap[ChineseAction].isValid = true;

	for (map<QAction*, Language>::iterator lang_it = mLanguageMap.begin();
			lang_it != mLanguageMap.end();
			lang_it ++)
	{
		if (lang_it->second.isValid || lang_it->second.isDefault)
		{
			mLanguageActionGroup->addAction(lang_it->first);
			ui.menuLanguage->addAction(lang_it->first);
			if (OPT.language == lang_it->second.language)
				lang_it->first->setChecked(true);
		}
	}
	mLanguageActionGroup->setExclusive(true);

	//mLanguageMap["Chinese"].isDefault = false;
}

void ReciteGREWords::languageSelected(QAction* action)
{
	if (mLanguageMap[action].isDefault)
	{
		for (	map<QAction*, Language>::iterator lang_it = mLanguageMap.begin();
				lang_it != mLanguageMap.end();
				lang_it ++)
		{
			QApplication::removeTranslator(mLanguageMap[action].translator);
		}
		return;
	}

	if (mLanguageMap[action].isValid)
	{
		QApplication::installTranslator(mLanguageMap[action].translator);
		ui.retranslateUi(this);
		return;
	}
}*/

void ReciteGREWords::show()
{
	//cout<<"ReciteGREWords::show"<<endl;
	if (OPT.mainWindowStatus == Qt::WindowMaximized)
		QWidget::showMaximized();
	else
	{
		//QWidget::showNormal();
		this->setGeometry(
				OPT.mainWindowX, OPT.mainWindowY,
				OPT.mainWindowWidth, OPT.mainWindowHeight);
		QWidget::show();
	}

	//this->setFont(OPT.programFont);
	setProgramFont();
	mReciteWordsWidget->reciteDiaplay()->setFont(OPT.wordBrowserFont);
	mBrowseWordsWidget->browserDiaplay()->setFont(OPT.wordBrowserFont);
	openLoginDialog();
}

void ReciteGREWords::resizeEvent(QResizeEvent * event )
{
	recordGeometry();
}

void ReciteGREWords::moveEvent( QMoveEvent * event )
{
	recordGeometry();
}

void ReciteGREWords::recordGeometry()
{
	OPT.mainWindowStatus = this->windowState();
	QRect r = this->geometry();
	QPoint tl = r.topLeft();
	OPT.mainWindowX = tl.x();
	OPT.mainWindowY = tl.y();
	OPT.mainWindowWidth = r.width();
	OPT.mainWindowHeight = r.height();
}

void ReciteGREWords::login()
{
	mOpenListAct->setEnabled(true);
	mOpenAnalysisDialogAct->setEnabled(true);
	mResumeAct->setEnabled(true);
	//mStartAct->setEnabled(true);
	mPauseAct->setEnabled(true);
	mFinishedCurTaskAct->setEnabled(true);
	mShowLogApp->setEnabled(true);
	mZoomIn->setEnabled(true);
	mZoomOut->setEnabled(true);
	//mFindWordInDB->setEnabled(true);

	QString curTitle = windowTitle();
	if (curTitle.contains("--"))
		curTitle = curTitle.left(curTitle.indexOf("--"));
	setWindowTitle(curTitle + "--" + DB.mUsrMain);
}

void ReciteGREWords::openSettingDialog()
{
	float sizeRate = 0.6f;
	mSettingsDialog->setGeometry(
			OPT.mainWindowX+(1-sizeRate)*OPT.mainWindowWidth/2,
			OPT.mainWindowY+(1-sizeRate)*OPT.mainWindowHeight/2,
			OPT.mainWindowWidth*sizeRate,
			OPT.mainWindowHeight*sizeRate);
	mSettingsDialog->showThisDialog();
	setProgramFont();
	this->raise();
	this->activateWindow();
}

void ReciteGREWords::setProgramFont()
{
	this->setFont(OPT.programFont);

	mBrowseWordsWidget->browserDiaplay()->setFont(OPT.wordBrowserFont);
	mReciteWordsWidget->reciteDiaplay()->setFont(OPT.wordBrowserFont);

	mBrowseWordsWidget->setFont(OPT.programFont);
	mReciteWordsWidget->setFont(OPT.programFont);
	mLoginDialog->setFont(OPT.programFont);
	mSelectListDialog->setFont(OPT.programFont);
	mReciteAnalysisDialog->setFont(OPT.programFont);
	//mSettingsDialog->setFont(OPT.programFont);
	mExportDataDialog->setFont(OPT.programFont);
	mExportDataDialog->setFont(OPT.programFont);

	mImportData->setFont(OPT.programFont);
	mExportData->setFont(OPT.programFont);
	ui.actionAbout->setFont(OPT.programFont);
	ui.actionSettings->setFont(OPT.programFont);
}

void ReciteGREWords::openAboutThisProgram()
{
	cout<<"ReciteGREWords::openAboutThisProgram"<<endl;
}

void ReciteGREWords::openWinLogWidget()
{
	cout<<"ReciteGREWord::openWinLogWidget"<<endl;
	WLOG.show();
}

void ReciteGREWords::openSelectListDialog()
{
	//cout<<"ReciteGREWord::openSelectListDialog"<<endl;
	//WM.finishLastTask();
	//mReciteWordsWidget->hide();
	float sizeRate = 0.8f;
	mSelectListDialog->setGeometry(
			OPT.mainWindowX+(1-sizeRate)*OPT.mainWindowWidth/2,
			OPT.mainWindowY+(1-sizeRate)*OPT.mainWindowHeight/2,
			OPT.mainWindowWidth*sizeRate,
			OPT.mainWindowHeight*sizeRate);
	mSelectListDialog->showThisDialog();
	this->raise();
	this->activateWindow();
}

void ReciteGREWords::openAnalysisDialog()
{
	//WM.finishLastTask();
	//mReciteWordsWidget->hide();
	float sizeRate = 0.9f;
	mReciteAnalysisDialog->setGeometry(
			OPT.mainWindowX+(1-sizeRate)*OPT.mainWindowWidth/2,
			OPT.mainWindowY+(1-sizeRate)*OPT.mainWindowHeight/2,
			OPT.mainWindowWidth*sizeRate,
			OPT.mainWindowHeight*sizeRate);
	mReciteAnalysisDialog->showThisDialog();
	this->raise();
	this->activateWindow();
}

void ReciteGREWords::openExportDataDialog()
{
	//cout<<"ReciteGREWords::openExportDataDialog"<<endl;
	float widthSizeRate = 0.5f;
	float heightSizeRate = 0.8f;
	mExportDataDialog->setGeometry(
				OPT.mainWindowX+(1-widthSizeRate)*OPT.mainWindowWidth/2,
				OPT.mainWindowY+(1-heightSizeRate)*OPT.mainWindowHeight/2,
				OPT.mainWindowWidth*widthSizeRate,
				OPT.mainWindowHeight*heightSizeRate);
	mExportDataDialog->showThisDialog();
	this->raise();
	this->activateWindow();
}

void ReciteGREWords::openImportDataDialog()
{
	//cout<<"ReciteGREWords::openImportDataDialog"<<endl;
	float widthSizeRate = 0.5f;
	float heightSizeRate = 0.8f;
	mImportDataDialog->setGeometry(
				OPT.mainWindowX+(1-widthSizeRate)*OPT.mainWindowWidth/2,
				OPT.mainWindowY+(1-heightSizeRate)*OPT.mainWindowHeight/2,
				OPT.mainWindowWidth*widthSizeRate,
				OPT.mainWindowHeight*heightSizeRate);
	mImportDataDialog->showThisDialog();
	this->raise();
	this->activateWindow();
}

void ReciteGREWords::reciteTaskSelected()
{
	cout<<"ReciteGREWords::reciteTaskSelected"<<endl;
	mResumeAct->setEnabled(false);
	mBrowseWordsWidget->hide();
	mReciteWordsWidget->show();
	//setCentralWidget(mReciteWordsWidget);
	mMainBulletin->clear();
	//mReciteWordsWidget->show();
	mReciteWordsWidget->initReciteTask();

	if(mTimer)
		delete mTimer;
	mTimer = new QTimer(this);
	mTimer->start(1000);
	mStartTimer = true;
	QObject::connect(mTimer, SIGNAL(timeout()), this, SLOT(updateTimeElapsedDisplay()));
	//QObject::connect(mTimer, SIGNAL(timeout()), this, SLOT(updateMainBulletin()));

	if (WM.getTask().selectWordMethod == select_by_resume_list)
		mTime = QTime::fromString(WM.getCurTaskElapseTime());
	else
		mTime = QTime(0,0,0);
	//updateMainBulletin();
}

void ReciteGREWords::browseTaskSelected()
{
	cout<<"ReciteGREWords::browseTaskSelected"<<endl;
	mReciteWordsWidget->hide();
	mBrowseWordsWidget->show();
	mMainBulletin->clear();
	mBrowseWordsWidget->initWordBrowserTask();

	if(mTimer)
		delete mTimer;
	mTimer = new QTimer(this);
	mTimer->start(1000);
	mStartTimer = true;
	QObject::connect(mTimer, SIGNAL(timeout()), this, SLOT(updateTimeElapsedDisplay()));

	mTime = QTime(0,0,0);
}

void ReciteGREWords::pauseButtonPushed()
{
	//ui.mainTabWidget->setEnabled(false);
	centralWidget()->setEnabled(false);
	mOpenListAct->setEnabled(false);
	mOpenAnalysisDialogAct->setEnabled(false);
	//mResumeAct->setEnabled(false);
	mStartAct->setEnabled(true);
	mPauseAct->setEnabled(false);
	mShowLogApp->setEnabled(true);
	mExitApp->setEnabled(true);
	OPT.isPaused = true;
	mRefreshTimer->stop();
}

void ReciteGREWords::startButtonPushed()
{
	//ui.mainTabWidget->setEnabled(true);
	centralWidget()->setEnabled(true);
	mOpenListAct->setEnabled(true);
	mOpenAnalysisDialogAct->setEnabled(true);
	//mResumeAct->setEnabled(true);
	mStartAct->setEnabled(false);
	mPauseAct->setEnabled(true);
	mShowLogApp->setEnabled(true);
	mExitApp->setEnabled(true);
	OPT.isPaused = false;
	mRefreshTimer->start(OPT.refreshInterval);
}

void ReciteGREWords::spaceBarPressed()
{
	if (OPT.isPaused)
		mStartAct->trigger();
	else
		mPauseAct->trigger();
}

void ReciteGREWords::finishCurTask()
{
	mStartTimer = false;
	if (mBrowseWordsWidget->isVisible()) mBrowseWordsWidget->hide();
	if (mReciteWordsWidget->isVisible()) mReciteWordsWidget->hide();
	WM.finishLastTask();
	//mTime = QTime(0,0,0);
}

void ReciteGREWords::switchTask()
{
	cout<<"ReciteGREWords::switchTask"<<endl;
	if (mBrowseWordsWidget->isVisible())
	{
		if (!OPT.lockSwitchTask)
		{
			mBrowseWordsWidget->hide();
			mReciteWordsWidget->show();
			mReciteWordsWidget->initReciteTask();
		}
	}
	else if (mReciteWordsWidget->isVisible())
	{
		if (!OPT.lockSwitchTask)
		{
			mReciteWordsWidget->hide();
			mBrowseWordsWidget->show();
			mBrowseWordsWidget->startBrowser();
			mBrowseWordsWidget->initWordBrowserTask();
		}
	}
	else
	{
		cout<<"ReciteGREWords::switchTask-->Unexpected central widget"<<endl;
		assert(0);
	}
}

void ReciteGREWords::updateTimeElapsedDisplay()
{
	//cout<<"ReciteGREWords::updateTimeElapsedDisplay"<<endl;
	if (mStartTimer)
	{
		//cout<<"ReciteGREWords::updateTimeElapsedDisplay"<<endl;
		mTimeElapsedEditor->setTime(mTime);
		if(!mStartAct->isEnabled())
		{
			//cout<<"ReciteGREWords::updateTimeElapsedDisplay"<<endl;
			mTime = mTime.addSecs(1);
			WM.setCurTaskElapseTime(mTime.toString());
		}
	}
}

void ReciteGREWords::updateMainBulletin()
{
	cout<<"ReciteGREWords::updateMainBulletin"<<endl;
	mMainBulletin->clear();
	QString ActiveWordCount = QString(
			"<FONT COLOR='blue'>Active: <B>%1. </B></FONT>"
			).arg(WM.getActiveWordCount());
	QString RecitingWordCount = QString(
			"<FONT COLOR='#330066'>Reciting: <B>%1. </B></FONT>"
			).arg(WM.getRecitingWordCount());
	QString FinishedWordCount = QString(
			"<FONT COLOR='green'>Finished: <B>%1. </B></FONT>"
			).arg(WM.getActiveWordTotalFinishedCount());
	QString UnhitRevWordCount = QString(
			"<FONT COLOR='red'>To-Hit: <B>%1. </B></FONT>"
			).arg(WM.getUnhitReviewingWordCount());
	//QString wordStatus = QString(
	//		"<FONT COLOR='#000066'> Word Status: <B>%1. </B></FONT>"
	//		).arg(WM.getCurWord()->getWordStatusString());

	mMainBulletin->insertHtml(ActiveWordCount);
	mMainBulletin->insertHtml(RecitingWordCount);
	mMainBulletin->insertHtml(FinishedWordCount);
	mMainBulletin->insertHtml(UnhitRevWordCount);
	//mMainBulletin->insertHtml(wordStatus);
}


void ReciteGREWords::exitApp()
{
	//cout<<"ReciteGREWords::exitApp"<<endl;
	QCoreApplication::quit();
}

void ReciteGREWords::resume()
{
	WM.loadUnfinishedTask();
	if (WM.getResumeWordCount() > 0)
	{
		WordManagerTask task;
		task.taskType = TaskType_Recite;
		task.selectWordMethod = select_by_resume_list;
		task.listName = "";
		task.listType = none;
		WM.setTask(task);
		reciteTaskSelected();
		mResumeAct->setEnabled(false);
	}
	else
	{
		QString message="Resume word list is empty. Please start a new task.";
		MessageBox::showWarning(QMessageBox::Warning,QString("Warning!"),message,12,this);
	}
}

void ReciteGREWords::zoomIn()
{
	cout<<"ReciteGREWords::zoomIn"<<endl;
	if (mBrowseWordsWidget->isVisible())
	{
		int fontSize = OPT.wordBrowserFont.pointSize();
		fontSize++;
		OPT.wordBrowserFont.setPointSize(fontSize);
		mBrowseWordsWidget->browserDiaplay()->setFont(OPT.wordBrowserFont);
	}
	else if (mReciteWordsWidget->isVisible())
	{
		int fontSize = OPT.wordBrowserFont.pointSize();
		fontSize++;
		OPT.wordBrowserFont.setPointSize(fontSize);
		mReciteWordsWidget->reciteDiaplay()->setFont(OPT.wordBrowserFont);
	}
	else
	{
		return;
	}
}

void ReciteGREWords::zoomOut()
{
	cout<<"ReciteGREWords::zoomOut"<<endl;
	if (mBrowseWordsWidget->isVisible())
	{
		//QFont font = mBrowseWordsWidget->browserDiaplay()->currentFont();
		int fontSize = OPT.wordBrowserFont.pointSize();
		fontSize--;
		OPT.wordBrowserFont.setPointSize(fontSize);
		//OPT.wordBrowserFont = font;
		mBrowseWordsWidget->browserDiaplay()->setFont(OPT.wordBrowserFont);
	}
	else if (mReciteWordsWidget->isVisible())
	{
		int fontSize = OPT.wordBrowserFont.pointSize();
		fontSize--;
		OPT.wordBrowserFont.setPointSize(fontSize);
		mReciteWordsWidget->reciteDiaplay()->setFont(OPT.wordBrowserFont);
	}
	else
	{
		return;
	}
}

void ReciteGREWords::openLoginDialog()
{
	//cout<<"ReciteGREWords::openLoginDialog"<<endl;
	float sizeRate = 0.5f;
	mLoginDialog->setGeometry(
			OPT.mainWindowX+(1-sizeRate)*OPT.mainWindowWidth/2,
			OPT.mainWindowY+(1-sizeRate)*OPT.mainWindowHeight/2,
			OPT.mainWindowWidth*sizeRate,
			OPT.mainWindowHeight*sizeRate);
	mLoginDialog->exec();

	if (OPT.lastAccount.isEmpty())
	{
		//centralWidget()->setEnabled(false);
		finishCurTask();
		DB.mUsrMain.clear();
		mOpenListAct->setEnabled(false);
		mOpenAnalysisDialogAct->setEnabled(false);
		mRefreshTimer->stop();
	}
	else if (OPT.lastAccount == DB.mUsrMain)
		login();

	this->raise();
	this->activateWindow();
}

void ReciteGREWords::findWordInDB()
{
	QFont font = this->font();
	mSearchDBDialog->setFont(font);
	mSearchDBDialog->showSearchBar();
	mSearchDBDialog->show();
}

void ReciteGREWords::findWordInDB(QString key)
{
	QFont font = this->font();
	mSearchDBDialog->setFont(font);
	mSearchDBDialog->setSearchWord(key);
	mSearchDBDialog->search();
	mSearchDBDialog->hideSearchBar();
	mSearchDBDialog->show();
}

